package com.furtherstar.auth.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.furtherstar.auth.controller.dto.CorpQuery;
import com.furtherstar.auth.controller.dto.DeptTree;
import com.furtherstar.auth.controller.dto.DeptUserQuery;
import com.furtherstar.auth.dao.model.Corp;
import com.furtherstar.auth.dao.model.CorpDept;
import com.furtherstar.auth.service.CorpDeptService;
import com.github.pagehelper.PageSerializable;
import com.yummy.convention.exception.AssertUtil;
import com.yummy.convention.http.Response;
import com.yummy.web.security.bean.SimpleUser;

@RestController
@RequestMapping("/corp")
public class CorpDeptController {
	
	@Autowired
	private CorpDeptService corpDeptService;
	
	@PostMapping("/list")
	public Response<List<Corp>> getCorpList(@RequestBody CorpQuery corpQuery){
		return Response.success(corpDeptService.queryCorpList(corpQuery));
	}
	
	@PostMapping("/list/available")
	public Response<List<Corp>> getAvaliableCorpList(@RequestBody CorpQuery corpQuery){
		return Response.success(corpDeptService.getAvailableCorpList(corpQuery));
	}
	
	@PostMapping("/add")
	public Response<Long> addCorp(@RequestBody Corp corp){
		return Response.success(corpDeptService.addCorp(corp));
	}
	
	@PostMapping("/update")
	public Response<Long> updateCorp(@RequestBody Corp corp){
		return Response.success(corpDeptService.updateCorp(corp));
	}
	
	@PostMapping("/disable")
	public Response<Long> disableCorp(@RequestBody Corp corp){
		return Response.success(corpDeptService.disableCorp(corp.getId()));
	}
	
	@PostMapping("/enable")
	public Response<Long> enableCorp(@RequestBody Corp corp){
		return Response.success(corpDeptService.enableCorp(corp.getId()));
	}
	
	@PostMapping("/delete")
	public Response<Boolean> deleteCorp(@RequestBody Corp corp){
		return Response.success(corpDeptService.deleteCorp(corp.getId()));
	}

	@GetMapping("/dept/tree/available")
	public Response<List<DeptTree>> getAvailableCorpDeptTree(Long corpId){
		AssertUtil.notNull(corpId,"corpId不能为空");
		return Response.success(corpDeptService.getAvailableCorpDeptTree(corpId));
	}

	@GetMapping("/dept/tree/get")
	public Response<List<DeptTree>> getCorpDeptTree(Long corpId){
		AssertUtil.notNull(corpId,"corpId不能为空");
		return Response.success(corpDeptService.getCorpDeptTree(corpId));
	}

	@GetMapping("/dept/get")
	public Response<CorpDept> getDept(Long deptId){
		AssertUtil.notNull(deptId,"deptId不能为空");
		return Response.success(corpDeptService.getDept(deptId));
	}

	@PostMapping("/dept/add")
	public Response<Long> addDept(@RequestBody CorpDept dept){
		AssertUtil.isTrue(StringUtils.isNotBlank(dept.getName()),"部门名称不能为空");
		return Response.success(corpDeptService.add(dept));
	}

	@PostMapping("/dept/update")
	public Response<Long> updateDept(@RequestBody CorpDept dept){
		AssertUtil.notNull(dept.getId(),"id不能为空");
		return Response.success(corpDeptService.update(dept));
	}

	@PostMapping("/dept/enable")
	public Response<Long> enable(@RequestBody CorpDept dept){
		AssertUtil.notNull(dept.getId(),"id不能为空");
		return Response.success(corpDeptService.enable(dept.getId()));
	}

	@PostMapping("/dept/disable")
	public Response<Long> disable(@RequestBody CorpDept dept){
		AssertUtil.notNull(dept.getId(),"id不能为空");
		return Response.success(corpDeptService.disable(dept.getId()));
	}

	@PostMapping("/dept/delete")
	public Response<Boolean> deleteDept(@RequestBody CorpDept dept){
		AssertUtil.notNull(dept.getId(),"id不能为空");
		return Response.success(corpDeptService.delete(dept.getId()));
	}

	@PostMapping("/dept/user/page")
	public Response<PageSerializable<SimpleUser>> pageDeptUsers(@RequestBody DeptUserQuery deptUserQuery){
		AssertUtil.notNull(deptUserQuery.getDeptId(),"deptId不能为空");
		return Response.success(corpDeptService.queryDeptUsers(deptUserQuery));
	}
}
